/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.config;

import java.io.File;
import java.io.InputStream;
import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.digester.Digester;

import com.bluestone.scripts.Project;
import com.bluestone.util.Util;

/**
 * @author <a href="mailto:ming7655@gmail.com">Aaron</a>
 * 
 * 
 */
public class ContextPaser {
	private String defaulConfigPath = "config\\bluestone_cfg.xml";

	private Digester digester;

	public ContextPaser() {
		init();
	}

	private void init() {
		if (this.digester == null) {
			this.digester = new Digester();
		}
		addRules(this.digester);
		
	}

	private void addRules(Digester d) {
		if (d == null) {
			Util.warn("The Digester is null.");
		}

		String tag_action = "bluestone-cfg/actions/action";
		String tag_actionHandler = "bluestone-cfg/actionHandlers/actionHandler";
		String tag_expAttributesHandler = "bluestone-cfg/expAttributesHandlers/expAttributesHandler";

		d.addObjectCreate("bluestone-cfg", BluestoneConfig.class);
		d.addObjectCreate(tag_action, ActionConfig.class);
		d.addObjectCreate(tag_actionHandler, ActionHandlerConfig.class);
		d.addObjectCreate(tag_expAttributesHandler,
				ExpAttributesHandlerConfig.class);
		// add rules for action
		d.addSetProperties(tag_action, "type", "type");
		d.addSetProperties(tag_action, "class", "className");

		// add rules for actionHandler
		d.addSetProperties(tag_actionHandler, "type", "type");
		d.addSetProperties(tag_actionHandler, "class", "className");

		// add rules for expAttributesHandler
		d.addSetProperties(tag_expAttributesHandler, "target", "target");
		d.addSetProperties(tag_expAttributesHandler, "class", "className");

		d.addSetNext(tag_action, "putActionConfig");
		d.addSetNext(tag_actionHandler, "putActionHandlerConfig");
		d.addSetNext(tag_expAttributesHandler, "putExpAttributesHandlerConfig");
	}

	public void parseConfig() {
		BluestoneConfig defautConfig = defaultParse();
		ConfigUtil.getInstance().registerBluestoneConfig(defautConfig);
		//Object obj = parse(defaulConfigPath);
		//if (BluestoneConfig.class.isInstance(obj)) {
		//	ConfigUtil.getInstance().registerBluestoneConfig(
		//			(BluestoneConfig) obj);
		//}

	}

	private BluestoneConfig defaultParse() {
		Object obj = parse("com/bluestone/core/config/bluestone_cfg.xml");
		if (BluestoneConfig.class.isInstance(obj)) {
			return (BluestoneConfig) obj;
		}
		return null;
	}
	
	private Object parse(String filePath) {
		try {		
			InputStream is = this.getClass().getClassLoader().getResourceAsStream(filePath);
			if(is!=null){
				return digester.parse(is);
			}
			URL url = this.getClass().getClassLoader().getResource(filePath);
			if(url==null){
				Util.warn("The bluestone_cfg.xml is not config:"+filePath);
				return null;
			}
			File input = new File(url.getPath());
			JAXBContext context = JAXBContext.newInstance(BluestoneConfig.class);
			Unmarshaller  unmarshaller = context.createUnmarshaller();
			return unmarshaller.unmarshal(input);
			//return digester.parse(input);
		} catch (Exception e) {
			Util.error("load bluestone config failure "+e);
		}
		return null;
	}

	public static void main(String[] args) {
		ContextPaser cp = new ContextPaser();
		cp.parseConfig();
	}
}
